<?php
declare (strict_types = 1);

namespace app\common\system\logic;

use app\common\BaseLogic;
use app\common\system\model\AuthNode;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

class AuthNodeLogic extends BaseLogic {

    public function getMenuItems()
    {
        $where = [];
        $where[] = ['is_menu', '=', '1'];
        $where[] = ['status', '=', '1'];
        $model = new AuthNode();
        $list = $model->where($where)->order('sort')->select();
        if ($list->isEmpty()) {
            return [];
        } else {
            return $list->toArray();
        }
    }

    public function getAdminNode()
    {
        $where = [];
        $where[] = ['status', '=', 1];
        $where[] = ['is_menu', '<>', 1];
        $model = new AuthNode();
        $list = $model->where($where)->select();

        return $list->toArray();

    }

    public function getNodeByIds($ids)
    {
        $where = [];
        $where[] = ['status', '=', 1];
        $where[] = ['is_menu', '<>', 1];
        $where[] = ['id', 'in', $ids];
        $model = new AuthNode();
        $list = $model->where($where)->select();

        if ($list->isEmpty()) {
            return [];
        } else {
            return $list->toArray();
        }
    }

    public function getMenuByIds($ids)
    {
        $where = [];
        $where[] = ['status', '=', 1];
        $where[] = ['is_menu', '=', 1];
        $where[] = ['id', 'in', $ids];
        $model = new AuthNode();
        $list = $model->where($where)->order('sort,id')->select();

        if ($list->isEmpty()) {
            return [];
        } else {
            return $list->toArray();
        }
    }

    /**
     * @param $ids array 节点ID
     * @return array 节点信息集合
     */
    public function getFuncByIds(array $ids)
    {
        $where = [];
        $where[] = ['status', '=', 1];
        $where[] = ['is_menu', '=', 3];
        $where[] = ['id', 'in', $ids];
        $model = new AuthNode();
        try {
            return $model->field('id,title,name')->where($where)->select();
        } catch (DataNotFoundException|ModelNotFoundException|DbException $e) {
            return [];
        }

    }
}